package com.jingdianjichi.subject.domain.handler.subject;

import com.jingdianjichi.subject.common.enums.IsDeleteFlagEnum;
import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import com.jingdianjichi.subject.domain.convert.MultipleSubjectConverter;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBO;
import com.jingdianjichi.subject.domain.handler.SubjectTypeHandler;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMultiple;
import com.jingdianjichi.subject.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class MultipleTypeHandler implements SubjectTypeHandler {

    @Resource
    SubjectMultipleService subjectMultipleService;
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBo) {

        ArrayList<SubjectMultiple> multipleList = new ArrayList<>();
        //就是answer的list
        List<SubjectAnswerBO> answerList = subjectInfoBo.getOptionList();
        if (!answerList.isEmpty()) {
            answerList.forEach(answer-> {
                SubjectMultiple convertAnswer = MultipleSubjectConverter.INSTANCE.converBoToEntity(answer);
                SubjectMultiple multiple = new SubjectMultiple();
                multiple.setIsCorrect(convertAnswer.getIsCorrect());
                multiple.setOptionType(convertAnswer.getOptionType());
                multiple.setOptionContent(convertAnswer.getOptionContent());
                multiple.setSubjectId(subjectInfoBo.getId());
                multiple.setIsCorrect(IsDeleteFlagEnum.UN_DELETED.getCode());
                multipleList.add(multiple);
            });
        }
        subjectMultipleService.batchInster(multipleList);
    }

    @Override
    public SubjectOptionBO query(Integer subjectId) {
        SubjectMultiple multiple = new SubjectMultiple();
        multiple.setSubjectId(subjectId);
        List<SubjectMultiple> subjectMultipleList =  subjectMultipleService.queryBySubjectId(multiple);
        List<SubjectAnswerBO> answerBoList = MultipleSubjectConverter.INSTANCE.convertToBO(subjectMultipleList);
        SubjectOptionBO optionBo = new SubjectOptionBO();
        optionBo.setOptionList(answerBoList);
        return optionBo;
    }

}
